<?php

namespace app\manage\controller;


use app\common\validate\system\SystemApiValid;
use app\common\validate\system\SystemMenuValid;
use app\manage\loader\ManageServices;
use think\Exception;

/**
 * 系统菜单/接口
 */
class SystemMenu extends BaseApi
{
    /**
     * 菜单列表
     */
    public function menu_list()
    {
        try {
            $data = $this->request->get();
            $options = $this->formatOption($data);
            $options["order"] = "order_id desc";
            $map = [];
            $parentId = !empty($data["pid"]) ? $data["pid"] : 0;
            $map[] = ["pid", "=", $parentId];
            $options["where"] = $map;
            $list = ManageServices::SystemMenu()->menuList($options);
            api_result(API_SUCCESS, "", $list);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 菜单列表
     */
    public function menu_tree_list()
    {
        try {
            $options["order"] = "order_id desc";
            $list = ManageServices::MerchantMenu()->menuLists($options, true);
            api_result(API_SUCCESS, "", $list);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 菜单信息
     */
    public function menu_info()
    {
        try {
            $id = $this->request->get("id");
            if (empty($id)) {
                api_exception(400, 'ID不能为空');
            }
            $info = ManageServices::SystemMenu()->menuInfo(['where' => ['id' => $id]]);
            api_result($info);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 菜单添加修改
     */
    public function menu_save()
    {
        try {
            $data = $this->request->post();
            $scene = '.add';
            if (!empty($data['id'])) {
                $scene = '.edit';
            }
            $this->validate($data, SystemMenuValid::class . $scene);
            ManageServices::SystemMenu()->menuSave($data);
            api_result(API_SUCCESS, "保存成功");
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 修改字段
     */
    public function menu_filed_save()
    {
        try {
            $data = $this->request->post();
            $this->validate($data, SystemMenuValid::class . '.edit');
            ManageServices::SystemMenu()->menuSave($data);
            api_result(API_SUCCESS, "保存成功" , ['id' => $data['id']]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 菜单删除
     */
    public function menu_del()
    {
        try {
            $id = $this->request->post("id");
            if (empty($id)) {
                api_result(API_ERROR, "要删除的权限ID不能为空");
            }
            ManageServices::SystemMenu()->menuDel($id);
            api_result(API_SUCCESS, "删除成功");
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 接口列表
     */
    public function interface_list()
    {
        try {
            $data = $this->request->get();
            $options = $this->formatOption($data);
            $options["order"] = "sort desc";
            $map = [];
            $type = isset($data["type"]) && $data["type"] > 0 ? $data["type"] : 2;
            $map[] = ["type_id", "=", $type];
            if (isset($data["pid"]) && $data["pid"] !== 'all') {
                $map[] = ["pid", "=", empty($data["pid"]) ? 0 : $data["pid"]];
            }
            if (!empty($data['keyword'])) {
                $map[] = ["title", "like", 'like', '%' . trim($data['keyword']) . '%'];
            }
            $options["where"] = $map;
            $list = ManageServices::SystemMenu()->interfaceList($options, (int)$type);
            api_result(API_SUCCESS, "", $list);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 接口权限列表
     */
    public function interface_auth_list()
    {
        try {
            $data = $this->request->get();
            $options = $this->formatOption($data);
            $options["order"] = "sort desc";
            $map = [];
            $pid = isset($data["pid"]) && $data["pid"] !== '' ? $data["pid"] : 0;
            $map[] = ["type_id", "=", 0];
            $map[] = ["pid", "=", $pid];
            $options["where"] = $map;
            $list = ManageServices::SystemMenu()->interfaceList($options);
            api_result(API_SUCCESS, "", $list);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 接口信息
     */
    public function interface_info()
    {
        try {
            $id = $this->request->get("id");
            if (empty($id)) {
                api_exception(400, 'ID不能为空');
            }
            $info = ManageServices::SystemMenu()->interfaceInfo(['where' => ['id' => $id]]);
            api_result($info);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 接口添加修改
     */
    public function interface_save()
    {

        try {
            $data = $this->request->post();
            $scene = '.add';
            if (!empty($data['id'])) {
                $scene = '.edit';
            }
            $this->validate($data, SystemApiValid::class . $scene);
            ManageServices::SystemMenu()->interfaceSave($data);
            api_result(API_SUCCESS, "保存成功");
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 接口删除
     */
    public function interface_del()
    {
        try {
            $id = $this->request->post("id");
            if (empty($id)) {
                api_result(API_ERROR, "接口ID不能为空");
            }
            ManageServices::SystemMenu()->interfaceDel($id);
            api_result(API_SUCCESS, "删除成功");
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * @title 重载接口
     * @login true
     */
    public function interface_overload()
    {
        try {
            $module = 'manage';
            ManageServices::SystemMenu()->interfaceOverload($module);
            api_result(API_SUCCESS, "重载成功");
        } catch (Exception $exception) {
            api_result($exception);
        }
    }


    /**
     * 更新接口某些字段
     */
    public function interface_fields_save(){
        $data = input('post.');
        try {
            $this->validate($data,SystemApiValid::class . '.interface_fields_save');
            ManageServices::SystemMenu()->interfaceSave($data);
            api_result(API_SUCCESS , '保存成功' , ['id' => $data['id']]);
        }catch (Exception $e){
            api_result($e);
        }
    }
}
